package client.whiteboard.view;

import javax.swing.JLayeredPane;
import javax.swing.JPanel;


import client.whiteboard.model.IDrawing;
import client.whiteboard.model.Line;
import client.whiteboard.model.Point;

import java.awt.*;
import java.awt.event.*;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Timer;

import client.whiteboard.controller.*;
public class Display extends JLayeredPane implements IDisplay
{
	private IDrawing _drawingData;
	private boolean _wiping;
	private int _wiperx;
	private WiperThread _wiperThread;
	public Display()
	{
		setVisible(true);
		setBounds(10, 10, 475, 440);
		_wiping = false;
		_wiperx = 0;
	}
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D)g;
		
			g.setColor(new Color(255, 255, 255));
			g.fillRect(0, 0, 475, 450);
			ListIterator pi = _drawingData.getDrawingIterator();
			while(pi.hasNext())
			{
				ListIterator li = ((LinkedList)pi.next()).listIterator();
				while(li.hasNext())
				{
					Line line = (Line)(li.next());
					Point p1 = line.getP1();
					Point p2 = line.getP2();
					g2d.setColor(new Color(90, 90, 90));
					BasicStroke stroke = new BasicStroke(line.getSize(), BasicStroke.JOIN_ROUND, BasicStroke.JOIN_BEVEL, 0);
					g2d.setStroke(stroke);
					g2d.drawLine(p1.getPx(), p1.getPy(), p2.getPx(), p2.getPy());
					g2d.setColor(new Color(90, 90, 90));
					g2d.fillOval(p1.getPx()-Math.round(line.getSize()/2), p1.getPy()-Math.round(line.getSize()/2), line.getSize(), line.getSize());
				}
			}
			
			if(_wiping)
			{
				BasicStroke wipeStroke = new BasicStroke(5, BasicStroke.JOIN_ROUND, BasicStroke.JOIN_BEVEL, 0);
				g2d.setStroke(wipeStroke);
				g2d.drawLine(_wiperThread.getWiperx(), 0, _wiperThread.getWiperx(), 500);
				g2d.setColor(new Color(255, 255, 255));
				g2d.drawRect(0, 0, _wiperThread.getWiperx(), 500);
				if(_wiperThread.getWiperx() > 500){
					_wiperThread.stop();
					_wiping = false;
					repaint();
				}
			}
		
	}
	public void setController(DisplayController displayController)
	{
		addMouseListener(displayController);
		addMouseMotionListener(displayController);
	}
	public void setDataInterface(IDrawing drawing)
	{
		_drawingData = drawing;
	}
	public void wipe()
	{
		_wiping = true;
		_wiperThread = new WiperThread();
		_wiperThread.setPaintTarget(this);
		_wiperThread.start();
		repaint();
	}
	public class WiperThread extends Thread
	{
		private JLayeredPane _paintTarget;
		private int _wiperx;
		public WiperThread()
		{
			_wiperx = 0;
		}
		public int getWiperx()
		{
			return _wiperx;
		}
		public void run() {
			while(_wiperx <= 500){
				if(_paintTarget != null){
					_paintTarget.repaint();
					_wiperx++;
					try {
						sleep(2);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				_wiperx++;
			}
		}
		public void setPaintTarget(JLayeredPane paintTarget)
		{
			_paintTarget = paintTarget;
		}
	}
	public void update() {
		repaint();
	}
}
